package com.shengde.jinwawaforleader.model.homePage;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.base.CommonBaseAdapter;
import com.shengde.jinwawaforleader.base.ViewHolder;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.network.BasexUtilsProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProAttendanceAllInfoList;
import com.shengde.jinwawaforleader.views.ChangeBirthdayDialog;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Calendar;
import java.util.List;

/**
 * 打卡记录
 * Created by Administrator on 2016/8/31 0031.
 */

public class AttendanceActivity extends BaseActivity implements AdapterView.OnItemClickListener {

	private static final String TAG = "AttendanceActivity";
	private static final int FINSH = 1;
	private TextView tv_title;
	private TextView tv_back;
	private TextView fill_add;
	private CommonBaseAdapter attentdanceAdapter;
	private ListView lv_attendance;
	private List<ProAttendanceAllInfoList.DataBean> lists;
	Calendar calendar;

	{
		calendar = Calendar.getInstance();
	}

	int years = calendar.get(Calendar.YEAR);
	int month = calendar.get(Calendar.MONTH)+1;
	int day = calendar.get(Calendar.DAY_OF_MONTH);
	String date = years + "-" + month + "-" + day;

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case FINSH:
					Log.i(TAG, "handleMessage: 123456789");
					if (attentdanceAdapter == null) {
						attentdanceAdapter = new CommonBaseAdapter(AttendanceActivity.this, lists, R.layout.attendance_adapter_item) {
							@Override
							public void convert(ViewHolder holder, int position) {
								holder.setText(R.id.tv_class_name, lists.get(position).getSname()).
										setText(R.id.tv_teacher_num_info, "老师" + lists.get(position).getNumt() + "/" + lists.get(position).getAllt()).
										setText(R.id.tv_student_num_info, "学生" + lists.get(position).getNumstu() + "/" + lists.get(position).getAllstu());
							}
						};
						lv_attendance.setAdapter(attentdanceAdapter);
					} else {
						attentdanceAdapter.notifyDataSetChanged();
					}

					break;
			}
		}
	};

	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.attendance_activity);
	}

	@Override
	public void initView() {
		lv_attendance = (ListView) findViewById(R.id.lv_attendance);
		tv_title = (TextView) findViewById(R.id.tv_title);
		tv_back = (TextView) findViewById(R.id.tv_back);
		fill_add = (TextView) findViewById(R.id.fill_add);
	}

	@Override
	public void initData() {
		fill_add.setText(getString(R.string.calendar));
		tv_title.setText(getString(R.string.attendance));
		showAttendanceAllInfoList(date);
	}

	/**
	 * 考勤
	 * @param date
	 */
	private void showAttendanceAllInfoList(String date) {
		RequestParams params = BasexUtilsProtocol.sendHttpRequest(this,"/director/classListkq");
		params.addParameter("schoolid", User.getSchoolId(this));
		params.addParameter("date", date);
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProAttendanceAllInfoList pro_attendance_allinfo_list = new Gson().fromJson(result, ProAttendanceAllInfoList.class);
				Log.i(TAG, "onSuccess: code = " + pro_attendance_allinfo_list.getCode());
				if (pro_attendance_allinfo_list != null && pro_attendance_allinfo_list.getCode() == 0) {
					lists = pro_attendance_allinfo_list.getData();
					mHandler.sendEmptyMessage(FINSH);
				}

			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
				Log.i(TAG, "onError: ex = "  +ex);

			}

			@Override
			public void onCancelled(CancelledException cex) {

			}

			@Override
			public void onFinished() {
				BasexUtilsProtocol.dialog.cancel();
			}
		});
	}

	@Override
	public void initListener() {
		tv_back.setOnClickListener(this);
		fill_add.setOnClickListener(this);
		lv_attendance.setOnItemClickListener(this);

	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.tv_back:
				finish();
				break;
			case R.id.fill_add:
				ChangeBirthdayDialog mChangeBirthDialog = new ChangeBirthdayDialog(
						AttendanceActivity.this);
				mChangeBirthDialog.setDate(years, month, day);
				mChangeBirthDialog.show();
				mChangeBirthDialog.setBirthdayListener(new ChangeBirthdayDialog.OnBirthListener() {
					@Override
					public void onClick(String year, String month, String day) {
						// TODO Auto-generated method stub
						int intMonth = Integer.parseInt(month);
						int intDay = Integer.parseInt(day);
						if (0 < intMonth && intMonth < 10) {
							month = "0" + month;
						}
						if (0 < intDay && intDay < 10) {
							day = "0" + day;
						}
						date = year + "-" + month + "-" + day;
						Toast.makeText(AttendanceActivity.this,
								year + "-" + month + "-" + day,
								Toast.LENGTH_LONG).show();
						showAttendanceAllInfoList(date);
					}
				});
				break;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
		Intent intent = new Intent(this, AttendanceClassActivity.class);
		intent.putExtra("classid", lists.get(position).getSid());
		intent.putExtra("className", lists.get(position).getSname());
		intent.putExtra("date",date);
		startActivity(intent);
	}
}
